package com.github.baichuan.file.rest.config;

import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(value = "repository.name", havingValue = "minio")
@Slf4j
public class Minio {
    /**
     * API调用地址ip
     */
    @Value("${repository.minio.ip}")
    private String ip;

    /**
     * API调用地址端口
     */
    @Value("${repository.minio.port}")
    private int port;

    /**
     * 连接账号
     */
    @Value("${repository.minio.access-key}")
    private String accessKey;

    /**
     * 连接秘钥
     */
    @Value("${repository.minio.secret-key}")
    private String secretKey;

    /**
     * #如果是true，则用的是https而不是http,默认值是true
     */
    @Value("${repository.minio.secure}")
    private boolean secure = false;


    @Bean
    public MinioClient client() {
        log.info("File repository is minio.");

        MinioClient minioClient = MinioClient.builder()
                .endpoint(ip,
                        port,
                        secure)
                .credentials(accessKey, secretKey)
                .build();
        return minioClient;
    }
}
